package basics;

import org.junit.Test;
import org.springframework.cglib.core.Local;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalField;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2019/1/21 12:37 PM
 */
public class DateTest {

  @Test
  public void testOne() {
    long l = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
    long l2 = LocalDateTime.now().minusDays(1).toEpochSecond(ZoneOffset.UTC);
    long l3 = LocalDateTime.now().minusDays(2).toEpochSecond(ZoneOffset.UTC);
    System.out.println(l);
    System.out.println(l2);
    System.out.println(l3);
    System.out.println(new Date(0));
  }

  @Test
  public void testTwo() {
    System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
    System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
    System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.NOON));
    System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT));
  }

  @Test
  public void testThree() {
    long l = LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli();
    long l2 = LocalDateTime.now().minusDays(1).toInstant(ZoneOffset.UTC).toEpochMilli();
    long l3 = LocalDateTime.now().minusDays(2).toInstant(ZoneOffset.UTC).toEpochMilli();
    System.out.println(l);
    System.out.println(l2);
    System.out.println(l3);
  }

  @Test
  public void testFour() {
    final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    Instant instant = Instant.ofEpochMilli(1546905600000L);
    long l = Instant.now().toEpochMilli();
    System.out.println(l);//1548060989633
    String format = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong("1556595873001")), ZoneId.systemDefault())
        .format(FORMATTER);
    System.out.println(format);

//    LocalDateTime.parse("1548060989633", FORMATTER)
//        .atZone(ZoneId.systemDefault())
//        .toInstant()
//        .toEpochMilli();
  }

  @Test
  public void testFive() {
    System.out.println(LocalDateTime.of(LocalDate.now().minusDays(8), LocalTime.MIN).toInstant(ZoneOffset.UTC).toEpochMilli());
    System.out.println(LocalDateTime.of(LocalDate.now().minusDays(8), LocalTime.MAX).toInstant(ZoneOffset.UTC).toEpochMilli());
    System.out.println(LocalDateTime.ofInstant(Instant.ofEpochMilli(1559143636887L), ZoneId.systemDefault()));

    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    long l = LocalDateTime.parse("2019-05-29 15:24:59", formatter).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
//    LocalDateTime.of(LocalDate.parse())
    System.out.println(l);
  }

  @Test
  public void testSix() {
    Instant instant = Instant.ofEpochMilli(1564073661307L);
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    System.out.println(localDateTime);
  }

  @Test
  public void testSeven() {
    Instant instant = Instant.ofEpochSecond(1563943961L);
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    System.out.println(localDateTime);

    Instant instant2 = Instant.ofEpochSecond(1563944000L);
    LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault());
    System.out.println(localDateTime2);


  }

  @Test
  public void testEight() {
    long current = TimeUnit.SECONDS.toSeconds(System.currentTimeMillis());
    System.out.println(current);

    long l = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    long l2 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    System.out.println(l);
    System.out.println(l2);


    Instant instant = Instant.ofEpochMilli(current);
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    System.out.println(localDateTime);
  }
}
